# -*- coding: utf-8 -*-
import datetime
import os
from Utils.zentao.zentaoTools import zentaoTools
import sys
from PyQt5.QtWidgets import QApplication, QMainWindow
from PyQt5 import QtCore, QtWidgets

from api_tools import api_tools
from framework.utils.FTP_OP import FTP_OP
from framework.utils.read_ini import ParseConFile
from framework.android.androiddevice import ADB

cur_dir_path = os.path.abspath(os.path.dirname(__file__))
project_dir_path = os.path.abspath(os.path.dirname(os.path.abspath(cur_dir_path)))
from framework.utils.log_record_util import get_logger
logger = get_logger()

class Ui_ApiTestTool(object):

    def __init__(self, centralwidget):
        super().__init__()
        self.parse_ini = ParseConFile("./config/config.ini")
        self.logPath = self.parse_ini.get_option_value("other", "logpath")
        self.testapp = self.parse_ini.get_option_value("other", "testapp")
        logger.info("cur_dir_path:" + cur_dir_path)
        logger.info("project_dir_path:" + project_dir_path)
        self.projectPath = None
        self.apiTools = None
        self.isCaseLib = False
        self.platform = "Android"
        self.isC = False
        self.product = 'Z6'
        self.zentao_testtask_id = '2780'
        self.setupUi(centralwidget)
        self.addAction()

    def setProduct(self, product):
        tag = None
        if product == "Z6" or product == "Z6-suo806cn":
            self.platform = "Android"
            self.isC = False
            tag = "testcaseList"
        elif product == "M61":
            self.platform = "RTOS"
            self.isC = True
            tag = "testcaseList_RTOS"
        else:
            self.platform = "Linux"
            self.isC = True
            tag = "testcaseList_Linux"

        logger.info("tag:" + str(tag))

        self.projectPath = self.parse_ini.get_option_value(tag, "projectPath")
        logger.info("projectPath:" + str(self.projectPath))

        self.modules = self.parse_ini.get_option_value(tag, "modules").split(",")
        logger.info("moduls:" + str(list(self.modules)))
        self.comboBox_module.clear()
        self.comboBox_module.addItems(self.modules)

    def setupUi(self, centralwidget):
        self.centralwidget = centralwidget
        sizePolicy = QtWidgets.QSizePolicy(QtWidgets.QSizePolicy.Expanding, QtWidgets.QSizePolicy.Expanding)
        sizePolicy.setHorizontalStretch(0)
        sizePolicy.setVerticalStretch(0)
        sizePolicy.setHeightForWidth(self.centralwidget.sizePolicy().hasHeightForWidth())
        self.centralwidget.setSizePolicy(sizePolicy)
        self.centralwidget.setObjectName("centralwidget")
        self.layoutWidget = QtWidgets.QWidget(self.centralwidget)
        self.layoutWidget.setGeometry(QtCore.QRect(10, 10, 311, 561))
        self.layoutWidget.setObjectName("layoutWidget")
        self.verticalLayout = QtWidgets.QVBoxLayout(self.layoutWidget)
        self.verticalLayout.setSizeConstraint(QtWidgets.QLayout.SetDefaultConstraint)
        self.verticalLayout.setContentsMargins(5, 0, 5, 0)
        self.verticalLayout.setSpacing(10)
        self.verticalLayout.setObjectName("verticalLayout")
        self.connection = QtWidgets.QGroupBox(self.layoutWidget)
        sizePolicy = QtWidgets.QSizePolicy(QtWidgets.QSizePolicy.Preferred, QtWidgets.QSizePolicy.Preferred)
        sizePolicy.setHorizontalStretch(0)

        sizePolicy.setVerticalStretch(2)
        sizePolicy.setHeightForWidth(self.connection.sizePolicy().hasHeightForWidth())
        self.connection.setSizePolicy(sizePolicy)
        self.connection.setMinimumSize(QtCore.QSize(0, 0))
        self.connection.setFlat(False)
        self.connection.setObjectName("connection")
        self.label = QtWidgets.QLabel(self.connection)
        self.label.setGeometry(QtCore.QRect(10, 30, 54, 12))
        self.label.setObjectName("label")
        self.lineEdit_zentao_user = QtWidgets.QLineEdit(self.connection)
        self.lineEdit_zentao_user.setGeometry(QtCore.QRect(50, 30, 81, 20))
        self.lineEdit_zentao_user.setObjectName("lineEdit_zentao_user")
        self.label_3 = QtWidgets.QLabel(self.connection)
        self.label_3.setGeometry(QtCore.QRect(10, 60, 54, 12))
        self.label_3.setObjectName("label_3")
        self.lineEdit_zentao_passwd = QtWidgets.QLineEdit(self.connection)
        self.lineEdit_zentao_passwd.setEnabled(True)
        self.lineEdit_zentao_passwd.setGeometry(QtCore.QRect(50, 60, 81, 20))
        self.lineEdit_zentao_passwd.setEchoMode(QtWidgets.QLineEdit.Password)
        self.lineEdit_zentao_passwd.setObjectName("lineEdit_zentao_passwd")
        self.label_4 = QtWidgets.QLabel(self.connection)
        self.label_4.setGeometry(QtCore.QRect(160, 30, 54, 12))
        self.label_4.setObjectName("label_4")
        self.comboBox_zentao_product = QtWidgets.QComboBox(self.connection)
        self.comboBox_zentao_product.setGeometry(QtCore.QRect(220, 30, 69, 22))
        self.comboBox_zentao_product.setObjectName("comboBox_zentao_product")
        self.label_5 = QtWidgets.QLabel(self.connection)
        self.label_5.setGeometry(QtCore.QRect(160, 60, 54, 12))
        self.label_5.setObjectName("label_5")
        self.comboBox_zentao_taskid = QtWidgets.QComboBox(self.connection)
        self.comboBox_zentao_taskid.setGeometry(QtCore.QRect(220, 60, 69, 22))
        self.comboBox_zentao_taskid.setObjectName("comboBox_zentao_taskid")
        self.pushButton_zentao_connect = QtWidgets.QPushButton(self.connection)
        self.pushButton_zentao_connect.setGeometry(QtCore.QRect(50, 100, 75, 23))
        self.pushButton_zentao_connect.setObjectName("pushButton_zentao_connect")
        self.verticalLayout.addWidget(self.connection)
        self.uploadCase = QtWidgets.QGroupBox(self.layoutWidget)
        sizePolicy = QtWidgets.QSizePolicy(QtWidgets.QSizePolicy.Preferred, QtWidgets.QSizePolicy.Preferred)
        sizePolicy.setHorizontalStretch(0)
        sizePolicy.setVerticalStretch(3)
        sizePolicy.setHeightForWidth(self.uploadCase.sizePolicy().hasHeightForWidth())
        self.uploadCase.setSizePolicy(sizePolicy)
        self.uploadCase.setFlat(False)
        self.uploadCase.setObjectName("uploadCase")
        self.radioButton_isCaseLib = QtWidgets.QRadioButton(self.uploadCase)
        self.radioButton_isCaseLib.setGeometry(QtCore.QRect(20, 90, 121, 16))
        self.radioButton_isCaseLib.setObjectName("radioButton_isCaseLib")
        self.label_6 = QtWidgets.QLabel(self.uploadCase)
        self.label_6.setGeometry(QtCore.QRect(10, 30, 54, 12))
        self.label_6.setObjectName("label_6")
        self.comboBox_module = QtWidgets.QComboBox(self.uploadCase)
        self.comboBox_module.setGeometry(QtCore.QRect(40, 30, 121, 22))
        self.comboBox_module.setObjectName("comboBox_module")
        self.pushButton_case_startupload = QtWidgets.QPushButton(self.uploadCase)
        self.pushButton_case_startupload.setGeometry(QtCore.QRect(180, 110, 75, 23))
        self.pushButton_case_startupload.setObjectName("pushButton_case_startupload")
        self.radioButton_isCaseProduct = QtWidgets.QRadioButton(self.uploadCase)
        self.radioButton_isCaseProduct.setGeometry(QtCore.QRect(20, 130, 121, 16))
        self.radioButton_isCaseProduct.setObjectName("radioButton_isCaseProduct")
        self.pushButton_genarate_testcaseList = QtWidgets.QPushButton(self.uploadCase)
        self.pushButton_genarate_testcaseList.setGeometry(QtCore.QRect(180, 160, 111, 23))
        self.pushButton_genarate_testcaseList.setObjectName("pushButton_genarate_testcaseList")
        self.verticalLayout.addWidget(self.uploadCase)
        self.uploadResult = QtWidgets.QGroupBox(self.layoutWidget)
        sizePolicy = QtWidgets.QSizePolicy(QtWidgets.QSizePolicy.Preferred, QtWidgets.QSizePolicy.Preferred)
        sizePolicy.setHorizontalStretch(0)
        sizePolicy.setVerticalStretch(3)
        sizePolicy.setHeightForWidth(self.uploadResult.sizePolicy().hasHeightForWidth())
        self.uploadResult.setSizePolicy(sizePolicy)
        self.uploadResult.setAlignment(QtCore.Qt.AlignLeading|QtCore.Qt.AlignLeft|QtCore.Qt.AlignTop)
        self.uploadResult.setFlat(False)
        self.uploadResult.setObjectName("uploadResult")
        self.pushButton_result_startUpload = QtWidgets.QPushButton(self.uploadResult)
        self.pushButton_result_startUpload.setGeometry(QtCore.QRect(180, 110, 75, 23))
        self.pushButton_result_startUpload.setObjectName("pushButton_result_startUpload")
        self.label_2 = QtWidgets.QLabel(self.uploadResult)
        self.label_2.setGeometry(QtCore.QRect(10, 30, 54, 12))
        self.label_2.setObjectName("label_2")
        self.comboBox_connectMethod = QtWidgets.QComboBox(self.uploadResult)
        self.comboBox_connectMethod.setGeometry(QtCore.QRect(60, 30, 51, 22))
        self.comboBox_connectMethod.setObjectName("comboBox_connectMethod")
        self.label_7 = QtWidgets.QLabel(self.uploadResult)
        self.label_7.setGeometry(QtCore.QRect(10, 60, 54, 12))
        self.label_7.setObjectName("label_7")
        self.comboBox_usbPort = QtWidgets.QComboBox(self.uploadResult)
        self.comboBox_usbPort.setGeometry(QtCore.QRect(60, 60, 121, 22))
        self.comboBox_usbPort.setObjectName("comboBox_usbPort")
        self.pushButton_refreshPort = QtWidgets.QPushButton(self.uploadResult)
        self.pushButton_refreshPort.setGeometry(QtCore.QRect(180, 60, 31, 23))
        self.pushButton_refreshPort.setObjectName("pushButton_refreshPort")
        self.pushButton_updateCaseID = QtWidgets.QPushButton(self.uploadResult)
        self.pushButton_updateCaseID.setGeometry(QtCore.QRect(10, 100, 121, 23))
        self.pushButton_updateCaseID.setObjectName("pushButton_updateCaseID")
        self.pushButton_updateRunID = QtWidgets.QPushButton(self.uploadResult)
        self.pushButton_updateRunID.setGeometry(QtCore.QRect(10, 140, 121, 23))
        self.pushButton_updateRunID.setObjectName("pushButton_updateRunID")
        self.verticalLayout.addWidget(self.uploadResult)
        self.frame = QtWidgets.QFrame(self.centralwidget)
        self.frame.setGeometry(QtCore.QRect(330, 10, 461, 591))
        self.frame.setFrameShape(QtWidgets.QFrame.StyledPanel)
        self.frame.setFrameShadow(QtWidgets.QFrame.Raised)
        self.frame.setObjectName("frame")
        self.textBrowser_log = QtWidgets.QTextBrowser(self.frame)
        self.textBrowser_log.setGeometry(QtCore.QRect(0, 5, 460, 540))
        sizePolicy = QtWidgets.QSizePolicy(QtWidgets.QSizePolicy.Preferred, QtWidgets.QSizePolicy.Expanding)
        sizePolicy.setHorizontalStretch(0)
        sizePolicy.setVerticalStretch(0)
        sizePolicy.setHeightForWidth(self.textBrowser_log.sizePolicy().hasHeightForWidth())
        self.textBrowser_log.setSizePolicy(sizePolicy)
        self.textBrowser_log.setToolTip("")
        self.textBrowser_log.setFrameShadow(QtWidgets.QFrame.Plain)
        self.textBrowser_log.setLineWidth(10)
        self.textBrowser_log.setObjectName("textBrowser_log")
        self.pushButton_getLog = QtWidgets.QPushButton(self.frame)
        self.pushButton_getLog.setGeometry(QtCore.QRect(30, 550, 61, 23))
        self.pushButton_getLog.setObjectName("pushButton_getLog")
        self.pushButton_clear = QtWidgets.QPushButton(self.frame)
        self.pushButton_clear.setGeometry(QtCore.QRect(120, 550, 51, 23))
        self.pushButton_clear.setObjectName("pushButton_clear")
        self.retranslateUi()

        str_user, str_pwd = self.parse_ini.get_zentao_user()
        self.lineEdit_zentao_user.setText(str_user)
        self.lineEdit_zentao_passwd.setEchoMode(QtWidgets.QLineEdit.Password)
        self.lineEdit_zentao_passwd.setText(str_pwd)
        self.comboBox_connectMethod.addItems(["ftp", "usb"])
        products_tmp = self.parse_ini.get_all_options("productID")
        products = []
        for x in products_tmp:
            products.append(x.upper())
        self.comboBox_zentao_product.addItems(products)
        logger.info("products:" + str(products) + " product:" + self.product)

        self.product = products[0]
        self.setProduct(self.product)

        testtask_ids = self.parse_ini.get_option_value("productID", self.product).split(",")
        self.comboBox_zentao_taskid.addItems(testtask_ids)

        self.zentao_testtask_id = testtask_ids[0]
        logger.info("testtask_ids:" + str(testtask_ids) + " zentao_testtask_id:" + self.zentao_testtask_id)


    def retranslateUi(self):
        _translate = QtCore.QCoreApplication.translate
        self.connection.setTitle(_translate("ApiTestTool", "禅道登录"))
        self.label.setText(_translate("ApiTestTool", "用户名"))
        self.label_3.setText(_translate("ApiTestTool", "密码"))
        self.label_4.setText(_translate("ApiTestTool", "产品名称"))
        self.label_5.setText(_translate("ApiTestTool", "测试单ID"))
        self.pushButton_zentao_connect.setText(_translate("ApiTestTool", "连接测试"))
        self.uploadCase.setTitle(_translate("ApiTestTool", "测试用例上传"))
        self.radioButton_isCaseLib.setText(_translate("ApiTestTool", "上传到用例库"))
        self.label_6.setText(_translate("ApiTestTool", "模块"))
        self.pushButton_case_startupload.setText(_translate("ApiTestTool", "开始上传"))
        self.radioButton_isCaseProduct.setText(_translate("ApiTestTool", "上传到产品用例"))
        self.pushButton_genarate_testcaseList.setText(_translate("ApiTestTool", "生成testcaseList"))
        self.uploadResult.setTitle(_translate("ApiTestTool", "测试结果上传"))
        self.pushButton_result_startUpload.setText(_translate("ApiTestTool", "开始上传"))
        self.label_2.setText(_translate("ApiTestTool", "连接方式"))
        self.label_7.setText(_translate("ApiTestTool", "USB端口"))
        self.pushButton_refreshPort.setText(_translate("ApiTestTool", "刷新"))
        self.pushButton_updateCaseID.setText(_translate("ApiTestTool", "更新本地caseID列表"))
        self.pushButton_updateRunID.setText(_translate("ApiTestTool", "更新本地runID列表"))
        self.pushButton_getLog.setText(_translate("ApiTestTool", "获取日志"))
        self.pushButton_clear.setText(_translate("ApiTestTool", "clear"))

    def addAction(self):
        self.pushButton_zentao_connect.clicked.connect(self.onAction_pushButton_zentao_connect)
        self.radioButton_isCaseLib.toggled.connect(lambda: self.btnCasestate(self.radioButton_isCaseLib))
        self.radioButton_isCaseProduct.toggled.connect(lambda: self.btnCasestate(self.radioButton_isCaseProduct))
        self.comboBox_zentao_product.currentIndexChanged.connect(self.comboBoxProductState)
        self.comboBox_connectMethod.currentIndexChanged.connect(self.comboBoxConnectMethodState)
        self.pushButton_refreshPort.clicked.connect(self.comboBoxConnectMethodState)

        self.pushButton_getLog.clicked.connect(self.onAction_pushButton_getLog)
        self.pushButton_clear.clicked.connect(self.textBrowser_log.clear)
        self.pushButton_genarate_testcaseList.clicked.connect(self.onAction_pushButton_testcaseList)
        self.pushButton_case_startupload.clicked.connect(self.onAction_pushButton_startuploadCase)
        self.pushButton_result_startUpload.clicked.connect(self.onAction_pushButton_startuploadResult)
        self.pushButton_updateCaseID.clicked.connect(self.onAction_pushButton_updateCaseID)
        self.pushButton_updateRunID.clicked.connect(self.onAction_pushButton_updateRunID)

        self.radioButton_isCaseProduct.setChecked(True)

    def onAction_pushButton_zentao_connect(self):
        logger.info("onAction_pushButton_zentao_connect")
        zentao_user = self.lineEdit_zentao_user.text()
        zentao_passwd = self.lineEdit_zentao_passwd.text()
        logger.info("zentao_user:" + zentao_user)
        logger.info("zentao_passwd:" + zentao_passwd)

        self.appendLog("start connect to zentao.")
        if len(zentao_user) < 2 or len(zentao_passwd) < 1:
            self.appendLog("FAIL, invalid user or passwd:" + zentao_user + ", " + zentao_passwd)
            return

        self.zentaotool = zentaoTools(zentao_user, zentao_passwd, self.textBrowser_log)
        self.apiTools = api_tools(self.zentaotool)
        self.appendLog("\n")
        pass

    def onAction_pushButton_getLog(self):
        logger.info("onAction_pushButton_getLog")
        if not os.path.exists(self.logPath + "\\Results"):
            os.makedirs(self.logPath + "\\Results")
        if self.comboBox_connectMethod.currentText() == "ftp":
            ftp = FTP_OP("192.168.15.10", 'user1', 'yz123456', 21)
            ftp.ftp_connect()

            ftp_filename1 = 'test/log_api//test.log'
            ftp_filename2 = 'test/log_api//case_runned.txt'
            ftp_filename3 = 'test/log_api//test_pm.log'

            local_filename1 = self.logPath + '\\Results\\test.log'
            local_filename2 = self.logPath + '\\Results\\case_runned.txt'
            local_filename3 = self.logPath + '\\Results\\test_pm.log'

            # 下载文件至本地
            ftp.download_file(ftp_filename1, local_filename1)
            ftp.download_file(ftp_filename2, local_filename2)
            ftp.download_file(ftp_filename3, local_filename3)
            ftp.ftp_quit()
            self.appendLog("getLog from ftp ok.")
            self.appendLog("log path:" + local_filename1)
        elif self.comboBox_connectMethod.currentText() == "usb" and self.platform == "Android":
            devicename = self.comboBox_usbPort.currentText()
            os.system("adb -s %s pull /sdcard/Android/data/com.yz.smartpos.yztester/files/Results %s" % (
                devicename, self.logPath))
            self.appendLog("getLog from usb device " + devicename + " ok.")
            self.appendLog("log path:" + os.path.join(self.logPath, "Results"))
        elif self.comboBox_connectMethod.currentText() == "usb" and self.platform == "RTOS":
            testdir = "D:\\log_aip\\Results\\SERIAL\\"
            list = os.listdir(testdir)
            list.sort(key=lambda fn: os.path.getmtime(testdir + '\\' + fn))
            filetime = datetime.datetime.fromtimestamp(os.path.getmtime(testdir + list[-1]))
            filepath = os.path.join(testdir, list[-1])
            print("filepath:" + filepath)
            print("destpath:" + self.logPath + "\\Results\\")
            os.system("copy %s %s" % (filepath, self.logPath + "\\Results"))
            os.system("copy %s %s" % (self.logPath + "\\Results\\" + list[-1], self.logPath + "\\Results\\" + "test.log"))
        else:
            self.appendLog("Please choose connect methond!!!")
        pass

    def onAction_pushButton_testcaseList(self):
        logger.info("onAction_pushButton_testcaseList")
        if "ALL" == self.comboBox_module.currentText() or "Setting" == self.comboBox_module.currentText():
            self.appendLog("请选择除ALL和Setting以外的模块")
            return
        self.appendLog("\n\n")
        if self.platform == "Android":
            api_tools.test_java_fun(self.projectPath, self.comboBox_module.currentText(), self.modules, self.textBrowser_log)
        elif self.platform == "RTOS":
            api_tools.test_rtos_fun(self.projectPath, self.comboBox_module.currentText(), self.modules, self.textBrowser_log)
        else:
            pass
        pass
    def onAction_pushButton_startuploadCase(self):
        curModule = self.comboBox_module.currentText()
        self.appendLog("start upload Case, module:" + curModule)
        if self.apiTools is None:
            self.onAction_pushButton_zentao_connect()
        self.zentaotool.setProduct(self.comboBox_zentao_product.currentText())
        self.zentaotool.setIsCaseLib(self.isCaseLib)
        if curModule == "ALL":
            for module_tmp in self.modules:
                logger.info("moduls:" + module_tmp)
                if module_tmp != "ALL":
                    self.apiTools.upload_testcase_toZentao(module_tmp, self.isC, self.isCaseLib, self.projectPath,
                                                           self.textBrowser_log)
        else:
            self.apiTools.upload_testcase_toZentao(curModule, self.isC, self.isCaseLib, self.projectPath,
                                                   self.textBrowser_log)
        self.appendLog("upload case finish.\n")
        pass

    def onAction_pushButton_startuploadResult(self):
        testtask_id = self.comboBox_zentao_taskid.currentText()
        self.appendLog("start upload Result to " + testtask_id + ", connect method:" + self.comboBox_connectMethod.currentText())
        if not os.path.exists(self.logPath + r"\\Results"):
            os.makedirs(self.logPath + r"\\Results")
        if self.comboBox_connectMethod.currentText() == "ftp":
            ftp = FTP_OP("192.168.15.10", 'user1', 'yz123456', 21)
            ftp.ftp_connect()

            ftp_filename1 = 'test/log_api//test.log'
            ftp_filename2 = 'test/log_api//case_runned.txt'
            ftp_filename3 = 'test/log_api//test_pm.log'

            result_path = self.logPath + r"\\" + testtask_id + r'\\Results\\'
            if not os.path.exists(self.logPath + r"\\" + testtask_id):
                os.makedirs(self.logPath + r"\\" + testtask_id)

            if not os.path.exists(result_path):
                os.makedirs(result_path)
            local_filename1 = result_path + 'test.log'
            local_filename2 = result_path + 'case_runned.txt'
            local_filename3 = result_path + 'test_pm.log'

            # 下载文件至本地
            ftp.download_file(ftp_filename1, local_filename1)
            ftp.download_file(ftp_filename2, local_filename2)
            ftp.download_file(ftp_filename3, local_filename3)
            ftp.ftp_quit()
            logger.info("ftp")
            self.appendLog("getLog from ftp ok.")
            self.appendLog("log path:" + local_filename1)
        elif self.comboBox_connectMethod.currentText() == "usb" and self.platform == "Android":
            devicename = self.comboBox_usbPort.currentText()
            os.system("adb -s %s pull /sdcard/Android/data/com.yz.smartpos.yztester/files/Results %s" % (
                devicename, self.logPath + "\\" + testtask_id + "\\"))
            self.appendLog("getLog from usb device " + devicename + " ok.")
            self.appendLog("log path:" + os.path.join(self.logPath, "Results"))
        elif self.comboBox_connectMethod.currentText() == "usb" and self.platform == "RTOS":
            testdir = "D:\\log_aip\\Results\\SERIAL\\"
            list = os.listdir(testdir)
            list.sort(key=lambda fn: os.path.getmtime(testdir + '\\' + fn))
            filetime = datetime.datetime.fromtimestamp(os.path.getmtime(testdir + list[-1]))
            filepath = os.path.join(testdir, list[-1])
            result_path = self.logPath + "\\" + testtask_id + "\\" + "Results"
            if not os.path.exists(result_path):
                os.makedirs(result_path)
            print("filepath:" + filepath)
            print("destpath:" + result_path)
            os.system("copy %s %s" % (filepath, result_path))
            os.system("copy %s %s" % (result_path + "\\" + list[-1], result_path + "\\" + "test.log"))
        else:
            self.appendLog("Please choose connect methond!!!")

        if self.apiTools is None:
            self.onAction_pushButton_zentao_connect()
        self.zentaotool.setProduct(self.comboBox_zentao_product.currentText())
        self.zentaotool.setIsCaseLib(False)
        self.zentaotool.checkConnection()
        if self.platform == "Android":
            self.apiTools.test_result_upload_to_zentao(self.logPath,  testtask_id, self.textBrowser_log)
        else:
            self.apiTools.test_result_upload_to_zentao_rtos(self.logPath, testtask_id, self.textBrowser_log)
        self.appendLog("upload result finish.\n")
        pass

    def onAction_pushButton_updateCaseID(self):
        self.appendLog("start update case id.")
        testtask_id = self.comboBox_zentao_taskid.currentText()
        if self.apiTools is None:
            self.onAction_pushButton_zentao_connect()
        self.zentaotool.setProduct(self.comboBox_zentao_product.currentText())
        self.zentaotool.setIsCaseLib(False)
        self.apiTools.test_update_local_caseID(self.logPath, self.textBrowser_log)
        self.appendLog("update case id finish.\n")

    def onAction_pushButton_updateRunID(self):
        self.appendLog("start update run id.")
        testtask_id = self.comboBox_zentao_taskid.currentText()
        if self.apiTools is None:
            self.onAction_pushButton_zentao_connect()
        self.zentaotool.setProduct(self.comboBox_zentao_product.currentText())
        self.zentaotool.setIsCaseLib(False)
        self.apiTools.test_update_local_runID(self.logPath, testtask_id, self.textBrowser_log)
        self.appendLog("update run id finish.\n")
    def btnCasestate(self, btn):
        if btn.isChecked() is True:
            logger.info(btn.text() + "is selected")
            if btn.text() == "上传到用例库":
                self.isCaseLib = True
            else:
                self.isCaseLib = False
            logger.info("isCaseLib:" + str(self.isCaseLib))
    def comboBoxProductState(self):
        logger.info("comboBoxProductState")
        self.comboBox_zentao_taskid.clear()
        option = self.comboBox_zentao_product.currentText()
        value = self.parse_ini.get_option_value("productID", option)
        logger.info("comboBoxProductState:" + str(list(value.split(","))))
        self.comboBox_zentao_taskid.addItems(list(value.split(",")))
        self.setProduct(option)


    def comboBoxConnectMethodState(self):
        logger.info("comboBoxConnectMethodState:" + self.comboBox_connectMethod.currentText())
        if self.comboBox_connectMethod.currentText() == "usb" and self.platform == "Android":
            devices = ADB.list_device()
            self.appendLog("usbdevices:" + str(devices))
            if len(devices) > 0:
                self.comboBox_usbPort.clear()
                self.comboBox_usbPort.addItems(devices)
            pass
        else:
            self.comboBox_usbPort.clear()
    def appendLog(self, tmpStr):
        self.textBrowser_log.append(tmpStr)
        logger.info(tmpStr)


uiTool = None
if __name__ == '__main__':
    # 创建Qt应用程序实例
    app = QApplication(sys.argv)
    # 创建一个QWidget对象，作为主窗口
    window = QMainWindow()
    window.setObjectName("ApiTestTool")
    window.resize(800, 600)
    _translate = QtCore.QCoreApplication.translate
    window.setWindowTitle(_translate("ApiTestTool", "ApiTestTools"))

    centralwidget = QtWidgets.QWidget(window)

    uiTool = Ui_ApiTestTool(centralwidget)

    window.setCentralWidget(centralwidget)
    actionApiTestTools = QtWidgets.QAction(centralwidget)
    actionApiTestTools.setObjectName("actionApiTestTools")
    QtCore.QMetaObject.connectSlotsByName(centralwidget)

    window.show()

    # 运行Qt应用程序
    sys.exit(app.exec_())


def printMy(tmpStr):
    if uiTool is not None:
        uiTool.appendLog(tmpStr)
